package com.wl.controller;

import com.mysql.cj.util.StringUtils;
import com.wl.response.CommonReturnType;
import com.wl.service.SendSmsService;
import com.wl.service.impl.SendSmsServiceImpl;
import com.wl.util.MyUtil;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @program: treeHole
 * @author: 吴磊
 * @create: 2021-07-06 22:27
 */
@WebServlet("/SmsCode.action")
public class SendSmsServlet extends BaseServlet {

    private SendSmsService sendSmsService = new SendSmsServiceImpl();


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String op = request.getParameter("op");
        if (StringUtils.isNullOrEmpty(op)) {
            show404Common(request, response);
            return;
        }
        if ("getSmsCode".equals(op)) {
            getSmsCodeOp(request, response);
        } else if ("validationSmsCode".equals(op)) {
            validationSmsCodeOp(request, response);
        } else {
            show404Common(request, response);
        }
    }

    /**
     * 验证验证码
     *
     * @param request
     * @param response
     */
    @SneakyThrows
    private void validationSmsCodeOp(HttpServletRequest request, HttpServletResponse response) {
        String telPhone = request.getParameter("telPhone");
        String smsCode = request.getParameter("smsCode");
        Map<String, Object> map = sendSmsService.validationSmsCode(telPhone, smsCode);
        MyUtil.responseData(response, CommonReturnType.success(map));
    }

    /**
     * 获取验证码
     *
     * @param request
     * @param response
     */
    @SneakyThrows
    private void getSmsCodeOp(HttpServletRequest request, HttpServletResponse response) {
        String telPhone = request.getParameter("telPhone");
        Map<String, Object> map = sendSmsService.getSmsCode(telPhone);
        MyUtil.responseData(response, CommonReturnType.success(map));
    }
}
